package com.match.competition_apply.utils;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.match.competition_apply.entity.CompetitionInfo;
import com.match.competition_apply.entity.vo.FileVo;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;

public class CompetitionExcelUtil {
    public static void exportCompetitionExcel(HttpServletResponse response, FileVo fileVo, List<CompetitionInfo> competitionInfoList){
        //获取模板对象
        TemplateExportParams params = new TemplateExportParams("src/main/resources/CompetitionExcel.xlsx");
        List<Map<String, Object>> allMap = transitionAdaptiveMap(competitionInfoList);

        Map<String, Object> map = new HashMap<String,Object>(100);
        map.put("CompetitionList", allMap);
        //3.执行excel导出
        params.setSheetName(new String[]{fileVo.getTablesName()});
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        response.reset();
        response.setHeader("Access-Control-Expose-Headers","Content-Disposition");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/msword;charset=utf-8");
        try {
            response.setHeader("Content-disposition", "filename=" + java.net.URLEncoder.encode(fileVo.getTablesName()+".xlsx", "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        try {
                ServletOutputStream outputStream = response.getOutputStream();
                workbook.write(outputStream);
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }



    /**
     * 获取所有数据封装成mapList
     *
     */
    public static  List<Map<String, Object>> transitionAdaptiveMap(List<CompetitionInfo> competitionInfoList) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (CompetitionInfo competitionInfo : competitionInfoList) {
            Map<String, Object> mapOne = new HashMap<>();
            mapOne.put("competitionBegin", parseTimeToString(competitionInfo.getCompetitionBegin()));
            mapOne.put("competitionEnd",parseTimeToString(competitionInfo.getCompetitionEnd()));
            mapOne.put("id", competitionInfo.getId());
            mapOne.put("competitionName", competitionInfo.getCompetitionName());
            mapOne.put("entryLever", competitionInfo.getEntryLever());
            mapOne.put("competitionLever", competitionInfo.getCompetitionLever());
            mapOne.put("competitionType", competitionInfo.getCompetitionType());
            mapOne.put("directoryInOut", competitionInfo.getDirectoryInOut());
            mapOne.put("newAdd", competitionInfo.getNewAdd());
            mapOne.put("rewardsBureau", competitionInfo.getRewardsBureau());
            mapOne.put("competitionDepartment", competitionInfo.getCompetitionDepartment());
            mapOne.put("competitionOrganizer", competitionInfo.getCompetitionOrganizer());
            mapOne.put("replaceCredits", competitionInfo.getReplaceCredits());
            mapOne.put("replaceCourse", competitionInfo.getReplaceCourse());
            mapOne.put("queueCount", competitionInfo.getQueueCount());
            mapOne.put("queuePersonCount", competitionInfo.getQueuePersonCount());
            mapList.add(mapOne);
        }
        return mapList;
    }
    /**
     * 时间(yyyy-MM-dd HH:mm:ss):Date格式转String格式
     */
    public static String parseTimeToString(Date date) {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }
}
